package atomlist;

import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @功能
 * @创建时间 2019/12/21
 * 自动阻塞，自动生产消费，无界队列
 */
public class T05_LinkedBlockingQueue {
    static BlockingQueue<String> strs = new LinkedBlockingQueue<>();
    static Random r = new Random();

    public static void main(String[] args) {
        new Thread(()->{
            for(int i =0;i<100;i++){
                try{
                    strs.put("a"+i); //如果满了，就会等待
                    TimeUnit.MICROSECONDS.sleep(r.nextInt(1000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"p1").start();
        for(int i=0;i<5;i++){
            new Thread(()->{
                for(;;){
                    try{
                        System.out.println(Thread.currentThread().getName()+"take-"+strs.take());  //如果空了激素会执行
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            },"c"+i).start();
        }
    }
}
